package tr.edu.metu.is.mega.server.service;

import java.util.List;

import tr.edu.metu.is.mega.client.service.GwtMailService;
import tr.edu.metu.is.mega.common.UserNotFoundException;
import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.server.business.MailService;
import tr.edu.metu.is.mega.server.business.UserManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Implementation of GWT mail service.
 */
public class GwtMailServiceImpl extends RemoteServiceServlet implements GwtMailService {

    private static final long serialVersionUID = -4002240738492058337L;

    private final UserManager userManager;

    /**
     * Default constructor.
     */
    public GwtMailServiceImpl() {
        userManager = new UserManager();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void sendLoginInfo(String emailAddress) throws UserNotFoundException {
        UserDTO userDTO = new UserDTO();
        userDTO.setEmail(emailAddress);
        List<UserDTO> users = userManager.getUsers(null, userDTO);
        if (users.size() != 1) {
            throw new UserNotFoundException();
        }
        MailService.sendLoginInfo(users.get(0));
    }

}
